/* $RCSFile: EcommerceLogin.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ECommerce/Secure/EcommerceLogin.aspx.cs $
 * $log$
 * Revision 21 2010/08/09 11:08:12  christian.surieux
 *   Added Log comment to all .ascx .aspx .master files
 * Revision 8 2010/08/09 10:07:05  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Diagnostics;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Core;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Authentication;
using AddonNice.Security;
using AddonNice.Settings;
using AddonNice.CRMWrapper;
using AddonNice.Helpers;

using AddonNice.ECommerce;

using Trace2    =   System.Diagnostics.Trace;
using AddonNice.ECommerce.Helpers;
using System.Collections.Generic;
using AddonNice.ECommerce.UI.WebControls;

namespace AddonNice.Secure.ECommerce
{
    public partial class ECommerceLogin : StaticStdAuxPage
    {
        
        public override int PageID
		{
			get
			{   return StStr.PageECommerceLogin; 
            }
		}

        protected void Page_Load(object sender, EventArgs e)
        {
    		if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ECommerceLogin Page_Load BEG IsPostBack: {0}, IsAuthenticated: {1}, QueryString: '{2}'",
                    IsPostBack,Request.IsAuthenticated,Request.Url.Query),PageTraceSwitch.Sw.Info);
            
            if ( Request.IsAuthenticated )
            {
                LitTitle.Visible    =   false;
                pleaseLogon.Visible =   false;
                hltregister.Visible =   false;
                UseSslBtn.Visible   =   false;
            }
        }

        void ContinueBtn_Click(object sender,ImageClickEventArgs e)
        {
            RedirectBackToReferringPage();
        }

        void CancelBtn_Click(object sender,ImageClickEventArgs e)
        {
            PortalSecurity.PortalHome(true,this);
        }

        void UseSslBtn_Click(object sender,EventArgs e)
        {
    		if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]ECommerceLogin UseSslBtn_Click BEG.",PageTraceSwitch.Sw.Info);
//            if ( !Request.IsSecureConnection )
//                Path.SwitchToSsl();
            string hostName     =   HttpContext.Current.Request.Url.Host;
            Uri baseUri         =   new Uri(String.Format("https://{0}", hostName));
            HttpContext context = HttpContext.Current;
            string url          =   context.Request.Url.AbsolutePath ;
            string path         =   url;
            string query        =   context.Request.Url.Query;

            string newUrl       =   new Uri(baseUri, path+query).ToString();// + query;
            if ( PathTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Path ConnectionString GetAbsoluteUrl END newUrl: {0}, query: {1}.", newUrl,query),PathTraceSwitch.Sw.Info);
            context.Response.Redirect(newUrl,true);
        }

        protected override void OnPreRender(EventArgs e)
        {
            //hltregister.NavigateUrl =   string.Format("{0}/register.aspx?pageid={1}",portalSettings.PortalSecureWebLogonPath,StStr.RegisterPageID);
            hltregister.NavigateUrl =   HttpUrlBuilder.BuildUrl("~/register.aspx",true,DynPageLoc.Login,StStr.RegisterPageID);
            base.OnPreRender(e);
        }

        /// <summary>
        /// We changed the normal order, the pagelayout is loaded in PreInit to get skins
		/// and have opportunity to load modules in OnPreInit,  having correct theming
		/// Page_Load event is just here in cas we want statistics
        /// </summary>
		protected override void OnPreInit(EventArgs e)
        {
    		if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]ECommerceLogin OnPreInit BEG.",PageTraceSwitch.Sw.Info);
            MasterPage m            =   Master;
            string lg               =   LocalizeHelper.GetCurrentNeutralCultureName();
            ContinueBtn.SkinID      =   string.Format("continue_{0}",lg);
            CancelBtn.SkinID        =   string.Format("cancel_{0}",lg);
            // this triggers the loading of modules in respectives panes
            TPans.InitPanes(this);
            base.OnPreInit(e);
    		if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]ECommerceLogin OnPreInit END",PageTraceSwitch.Sw.Info);
        }

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
        private void InitializeComponent()
        {
            if ( Request.IsSecureConnection )
                UseSslBtn.Visible  =   false;
            else
                UseSslBtn.Click += new EventHandler(UseSslBtn_Click);
            Load                += new System.EventHandler(Page_Load);
            ContinueBtn.Click   += new ImageClickEventHandler(ContinueBtn_Click);
            CancelBtn.Click     += new ImageClickEventHandler(CancelBtn_Click);
        }


    }
}
